from django.test import TestCase

# Create your tests here.
import openpyxl
from openpyxl.cell import cell


def read_excel_dict(path: str):
    """读取excel数据，存储为字典"""
    # 实例化一个workbook
    workbook = openpyxl.load_workbook(path)
    # 实例化一个sheet
    sheet = workbook['student']
    # 定义一个变量存储最终的数据
    students = []
    # 准备keys
    keys = ['sno', 'name', 'gender', 'birthday', 'mobile', 'email', 'address']

    # 遍历
    for row in sheet.iter_rows(min_row=2, values_only=True):  # Assuming the data starts from the 2nd row
        # 定义一个临时的字典
        temp_dict = {}
        # 组合值和key
        for index, value in enumerate(row):
            # 组合
            temp_dict[keys[index]] = value
        # 附加到list中
        students.append(temp_dict)
    # 返回
    return students

if __name__ == '__main__':
    path = "C:\\student.xlsx"  # Escape backslashes in file path or use raw string (r"C:\student.xlsx")
    # 调用函数
    students = read_excel_dict(path)
    # 输出
    print(students)

